﻿using System;
using System.Configuration;

namespace MuNet
{
    public class MuConfig
    {
        private readonly Configuration s_cfg;

        public MuConfig()
        {
            this.s_cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }

        public MuConfig(string path)
        {
            this.s_cfg = ConfigurationManager.OpenExeConfiguration(path);
        }

        public MuConfig(Configuration cfg)
        {
            this.s_cfg = cfg;
        }

        /// <summary>
        /// 根据Key取Value值
        /// </summary>
        /// <typeparam name="T">返回的类型</typeparam>
        /// <param name="key">键名</param>
        /// <param name="defval">默认值</param>
        /// <returns></returns>
        public static T Get2<T>(string key, T defval)
        {
            object t_obj = ConfigurationManager.AppSettings[key];
            T t_out = defval;
            if (t_obj != null)
            {
                try
                {
                    string t_val = t_obj.ToString().Trim();
                    t_out = (T)(object)t_val;
                }
                catch (FormatException)
                {
                    t_out = default(T);
                }
            }
            return t_out;
        }

        /// <summary>
        /// 根据Key取Value值
        /// </summary>
        /// <typeparam name="T">返回的类型</typeparam>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public static T Get2<T>(string key)
        {
            T t_def = default(T);
            return Get2<T>(key, t_def);
        }

        /// <summary>
        /// 根据Key取Value值
        /// </summary>
        /// <typeparam name="T">返回的类型</typeparam>
        /// <param name="key">键名</param>
        /// <param name="defval">默认值</param>
        /// <returns></returns>
        public T Get<T>(string key, T defval)
        {
            object t_val = this.s_cfg.AppSettings.Settings[key].Value;
            T t_out = defval;
            if (t_val != String.Empty)
            {
                try
                {
                    //string t_val = t_obj.ToString().Trim();
                    //string t_str = "321";
                    //int t_int = int.Parse(t_str);
                    //t_out = (T)(object)t_val;

                    if (typeof(T) == typeof(int))
                    {
                        t_out = t_val as T;
                    }


                }
                catch (FormatException)
                {
                    t_out = default(T);
                }
            }
            return t_out;
        }

        /// <summary>
        /// 根据Key取Value值
        /// </summary>
        /// <typeparam name="T">返回的类型</typeparam>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            T t_def = default(T);
            return Get<T>(key, t_def);
        }

        /// <summary>
        /// 添加新的Key ，Value键值对
        /// </summary>
        /// <param name="key">键名</param>
        /// <param name="value">键值</param>
        public static void Add2(string key, string value)
        {
            ConfigurationManager.AppSettings.Add(key, value);
        }

        public void Add(string key, string value)
        {
            this.s_cfg.AppSettings.Settings.Add(key, value);
            this.s_cfg.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 根据Key修改Value
        /// </summary>
        /// <param name="key">要修改的Key</param>
        /// <param name="value">要修改为的值<</param>
        public static void Set2(string key, string value)
        {
            ConfigurationManager.AppSettings.Set(key, value);
        }

        public void Set(string key, string value)
        {
            this.s_cfg.AppSettings.Settings[key].Value = value;
            this.s_cfg.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 根据Key删除项
        /// </summary>
        /// <param name="key">要删除的键名</param>
        public static void Remove2(string key)
        {
            ConfigurationManager.AppSettings.Remove(key);
        }

        public void Remove(string key)
        {
            this.s_cfg.AppSettings.Settings.Remove(key);
            this.s_cfg.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
